import {ProColumns, ProTable} from "@ant-design/pro-components";
import {getSettingPage, removeSetting, SettingVO} from "@/services/SettingService";
import {Button, Popconfirm, Space} from "antd";
import React from "react";
import {useAccess} from "@@/exports";
import {SettingModel} from "@/models/setting";

export interface SettingProps {
    tableRef?: any;
}


const SettingTable = (props: SettingProps) => {
    const access = useAccess()
    let {actions: notifyActions} = SettingModel


    /**
     * 列定义
     */
    const columns: ProColumns<SettingVO>[] = [
        {
            title: "id",
            dataIndex: "id",
            width: 120,
            hideInTable: true,
            hideInSearch: true,
        },
        {
            title: "客资通知",
            dataIndex: "customerNotify",
            width: 120,
            hideInSearch: true,
        },
        {
            title: "订单通知",
            dataIndex: "orderNotify",
            width: 120,
            hideInSearch: true,
        },
        {
            title: '操作', render: (_: any, record: SettingVO) => [
                <Space key="operate">
                    {access.hasSettingUpdatePerm && <Button type="link" size="small"
                                                            onClick={() => notifyActions.openUpdateModal(true, record)}>编辑</Button>}
                    {access.hasSettingUpdatePerm && <Popconfirm
                        title="确定要删除吗"
                        onConfirm={() => {
                            removeSetting(record.id).then(() => props.tableRef?.current?.resetFields())
                        }}
                        okText="确认"
                        cancelText="取消"
                    >
                        <Button type="link" danger size="small">删除</Button>
                    </Popconfirm>}
                </Space>
            ],
            hideInSearch: true,
        }
    ]

    // 工具栏
    let actions = [
        access.hasSettingSavePerm &&
        <Button key="create" type="primary" onClick={() => notifyActions.openUpdateModal(true, undefined)}>添加</Button>
    ]

    return <>
        <ProTable
            actionRef={props.tableRef}
            columns={columns}
            request={getSettingPage}
            toolbar={{
                actions: actions
            }}
            scroll={{x: 500}}
            sticky={{offsetHeader: 55, offsetScroll: 5}}
        >
        </ProTable>
    </>
}

export default SettingTable
